﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A.HTMlServer.HThread
{
    public class ToThread
    {
        public static List<string> Llink = new List<string>();
        internal static DCommon dc = null;

        public delegate void ToHtmlDelegate(string url);
        private static ToHtmlDelegate tdelegate;

        public static ToHtmlDelegate Tdelegate
        {
            get { return ToThread.tdelegate; }
            set { ToThread.tdelegate = value; }
        }
        
        public static bool ThreadState = false;
        public static object ObjLock = new object();

        private static void Threadstart()
        {
            //这里的锁住是为了防止同一时间多个请求启动线程
            lock(ObjLock)
            {
                //委托为null时不启动线程
                if (!ThreadState&&Tdelegate!=null)
                {
                    dc = new DCommon();
                    for (var i = 0; i < 10; i++)
                    {
                        RunThread RT = new RunThread();
                        RT.Start(Tdelegate);
                    }
                    ThreadState = true;
                }
            }
        }

        public static void AddItem(string url)
        {
            Llink.Add(url);
            Threadstart();
        }
    }
}
